 let q = {
     pagenum: 1, // 页码值，默认请求第一页的数据
     pagesize: 5, // 每页显示几条数据，默认每页显示2条
     cate_id: '', // 文章分类的 Id
     state: '' // 文章的发布状态
 }

 function getArtList() {
     $.ajax({
         type: "get",
         url: "/my/article/list",
         data: q,
         success: function (res) {
             if (res.status !== 0) return layer.msg(res.message);
             let htmlstr = template("tpl-table", res)
             // return console.log(htmlstr);
             $("tbody").html(htmlstr)
             renderPage(res.total)
         }
     })
 }
 getArtList();
 template.defaults.imports.timeFormat = function (time) {
     let dt = new Date(time);
     let y = dt.getFullYear().toString();
     let m = (dt.getMonth() + 1).toString().padStart(2, '0');
     let d = dt.getDate().toString().padStart(2, '0');
     let h = dt.getHours().toString().padStart(2, '0');
     let min = dt.getMinutes().toString().padStart(2, '0');
     let s = dt.getSeconds().toString().padStart(2, '0');
     return `${y}-${m}-${d} ${h}:${min}:${s}`
 }

 function initData() {
     $.ajax({
         type: 'get',
         url: '/my/article/cates',
         success: function (res) {
             if (res.status !== 0) return layer.msg(res.message);
             let htmlstr = template("tpl-cate", res)
             $("[name=cate_id]").html(htmlstr)
             layui.form.render();
         }
     })
 }
 initData();
 $("#form-search").on("submit", function (e) {
     //  console.log(1);
     e.preventDefault();
     //  let str = $(this).serialize();
     //  return console.log(str);
     let cate_id = $('[name=cate_id]').val()
     let state = $('[name=state]').val()
     q.cate_id = cate_id;
     q.state = state;
     getArtList();
 })

 function renderPage(total) {
     layui.laypage.render({
         elem: 'page', // 分页容器的 Id
         count: total, // 总数据条数
         limit: q.pagesize, // 每页显示几条数据
         curr: q.pagenum, // 设置默认被选中的分页
         layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
         limits: [2, 5, 10, 15, 20, 30],
         jump: function (obj, first) {
             //  console.log(obj.curr)
             // 把最新的页码值，赋值到 q 这个查询参数对象中
             q.pagenum = obj.curr
             q.pagesize = obj.limit
             if (!first) {
                 getArtList();
             }
         }

     })
 }
 $('tbody').on("click", ".dete-btn", function () {
     let len = $(".dete-btn").length;
     let id = $(this).attr("data-id");
     layer.confirm('确认删除?', {
         icon: 3,
         title: '提示'
     }, function (index) {
         $.ajax({
             type: 'get',
             url: '/my/article/delete/' + id,
             success: function (res) {
                 if (res.status !== 0) layer.msg(res.message);
                 if (len === 1 && q.pagenum !== 1) {
                     q.pagenum = q.pagenum - 1;
                 }
                 layer.close(index)
                 getArtList();
             }
         })
     })

 })
 $('tbody').on('click', '.list_edit', function () {
     // 获取自定义属性的值
     const id = $(this).attr('data-id')
     location.href = '/article/art_edit.html?id=' + id
 })